﻿using System;
using System.Linq;
using System.Web.Mvc;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.WEB.GIMS.Class;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    // [AccessDeniedAuthorize]
    public class MasDepreciationTypeController : Controller
    {
        //
        // GET: /MasDepreciationType/

        public ActionResult Index()
        {
            var service = new MasDepreciationTypeService.MasDepreciationTypeServiceClient();
            return View(service.SelectMasDepreciationType(new MasDepreciationType { RecordStatus = "!D" }, null));
        }


        public ActionResult Create()
        {
            var model = new MasDepreciationType();
            model.Name = "";
            model.DpRateYear = 0;
            model.MinLifeYear = 0;
            model.MaxLifeYear = 0;
            model.MinRateYear = 0;
            model.MaxRateYear = 0;
            model.RecordStatus = "U";
            model.CreatedBy = GimsMembership.CurrentEmpId;
            model.CreatedWhen = DateTime.Now;
            model.UpdatedBy = GimsMembership.CurrentEmpId;
            model.UpdatedWhen = DateTime.Now;
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(MasDepreciationType model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new MasDepreciationTypeService.MasDepreciationTypeServiceClient();
                    int id = service.Insert(model, Class.GimsMembership.CurrentEmpId);

                    return RedirectToAction("Details", new {id});

                }
                else
                {
                    string em = "";
                    foreach (ModelState modelState in ViewData.ModelState.Values)
                    {
                        foreach (ModelError error in modelState.Errors)
                        {
                            em += modelState.Value + ", "+ error.Exception + ", " + error.ErrorMessage;
                        }
                    }
                    ViewBag.Msg = em;
                }
            }
            catch (Exception ex)
            {
                ViewBag.Msg = ex.Message;
            }

            return View(model);
        }

        public ActionResult Edit(int id)
        {
            var service = new MasDepreciationTypeService.MasDepreciationTypeServiceClient();
            var item = service.SelectMasDepreciationType(new MasDepreciationType { DpTypeId = id }, null).FirstOrDefault();

            if (item == null)
            {
                return HttpNotFound();
            }

            return View(item);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(MasDepreciationType model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new MasDepreciationTypeService.MasDepreciationTypeServiceClient();
                    int id = service.Update(model, Class.GimsMembership.CurrentEmpId);

                    return RedirectToAction("Details", new { id });
                }
                else
                {
                    string em = "";
                    foreach (ModelState modelState in ViewData.ModelState.Values)
                    {
                        foreach (ModelError error in modelState.Errors)
                        {
                            em += modelState.Value + ", " + error.Exception + ", " + error.ErrorMessage;
                        }
                    }
                    ViewBag.Msg = em;
                }      
            }
            catch (Exception ex)
            {
                ViewBag.Msg = ex.Message;
            }
            return View(model);
        }

        public ActionResult Delete(int id)
        {
            try
            {
                var service = new MasDepreciationTypeService.MasDepreciationTypeServiceClient();
                service.Delete(id, Class.GimsMembership.CurrentEmpId);

                return RedirectToAction("Index");
            }
            catch
            {
                return Index();
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult DeleteAll(int[] deleteIds)
        {
            try
            {
                if (deleteIds != null)
                {
                    var service = new MasDepreciationTypeService.MasDepreciationTypeServiceClient();

                    foreach (int delete in deleteIds)
                    {
                        service.Delete(delete, Class.GimsMembership.CurrentEmpId);
                    }
                }

                ModelState.AddModelError("Error", "ลบข้อมูลไปทั้งหมด " + deleteIds.Length.ToString("#,##0") + " รายการ");

                return Json(new { success = true });
            }
            catch
            {
                ModelState.AddModelError("Error", "พบข้อผิดพลาดขณะลบข้อมูล");
                return Json(new { success = false });
            }
        }

        public ActionResult Details(int id)
        {
            var service = new MasDepreciationTypeService.MasDepreciationTypeServiceClient();
            var model = service.SelectMasDepreciationType(new MasDepreciationType { DpTypeId = id }, null).FirstOrDefault();
            return View(model);
        }

    }
}
